package com.usf.jprovider.schema.model.jmodel.item.collection;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;

import com.usf.jprovider.object.JObjectNode;
import com.usf.jprovider.object.builder.SQLBuilder;
import com.usf.jprovider.object.env.Environment;
import com.usf.jprovider.tools.RandomTools;

public final class List extends AbstractCollection{

	@XmlAttribute protected int min;
	@XmlAttribute protected int max;
	@XmlElement protected Table table;
	
	public List() {
		super();
	}
	public int getMin() {
		return min;
	}
	public int getMax() {
		return max;
	}
	public Table getTable() {
		return table;
	}
	
	@Override
	protected void _create(Environment env, JObjectNode node) throws Exception{
		int count = RandomTools.randomizeInteger(min, max);
		for(int i=0; i<count; i++)
			table.create(env, node);
	}
	@Override
	protected boolean _check(Environment env, JObjectNode node) throws Exception {
		JObjectNode[] nodes = node.childs();
		for(JObjectNode item : nodes)
			if(!table.checkNode(env, item))return false;	
		return true;
	}
	@Override
	protected void _persist(Environment env, JObjectNode node, SQLBuilder builder) throws Exception {
		JObjectNode[] nodes = node.childs();
		for(JObjectNode item : nodes)
			table.persistNode(env, item, builder);
	}
}
